package com.namecard.namecardapi.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.namecard.namecardapi.entity.Country;
import com.namecard.namecardapi.entity.DTO.LyUserReq;
import com.namecard.namecardapi.entity.Industry;
import com.namecard.namecardapi.entity.LyUser;
import com.namecard.namecardapi.entity.vo.CountryVO;
import com.namecard.namecardapi.entity.vo.ResponseVO;
import com.namecard.namecardapi.service.CountryService;
import com.namecard.namecardapi.service.DataSplitterService;
import com.namecard.namecardapi.service.IndustryService;
import com.namecard.namecardapi.service.LyUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 领英用户表(LyUser)表控制层
 *
 * @author makejava
 * @since 2024-08-06 13:38:34
 */
@RestController
@RequestMapping("/lyUser")
@Api(tags = "1、领英数据管理")
public class LyUserController {
    @Resource
    private DataSplitterService lyDataSplitterServiceImpl;
    
    @Resource
    private LyUserService lyUserService;
    @Resource
    private CountryService countryService;
    @Resource
    private IndustryService industryService;
    
    /**
     * 领英数据入库
     */
    @ApiOperation(value = "1、输入文件路径，解析入库")
    @PostMapping("/upload")
    public ResponseVO<String> dataReceipt(String filePath) throws IOException {
        HintManager hintManager = HintManager.getInstance();
        hintManager.setDataSourceName("ds-ly");
        lyDataSplitterServiceImpl.handleFile(filePath);
        return ResponseVO.getSuccessResponseVo("ok");
    }

    @ApiOperation(value = "2、创建索引")
    @GetMapping("/createIndex")
    public ResponseVO<String> createIndex() {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 指定数据源名称
            hintManager.setDataSourceName("ds-ly");

            // 执行存储过程
            lyUserService.createIndexes();

            return ResponseVO.getSuccessResponseVo("创建领英索引成功");
        } catch (Exception e) {
            throw new RuntimeException("已存在相同的领英索引，不要重复创建", e);
        }
    }

    @ApiOperation(value = "3、删除索引")
    @GetMapping("/dropIndex")
    public ResponseVO<String> dropIndex() {
        try (HintManager hintManager = HintManager.getInstance()) {
            // 指定数据源名称
            hintManager.setDataSourceName("ds-ly");

            // 执行存储过程
            lyUserService.dropIndexes();

            return ResponseVO.getSuccessResponseVo("删除领英索引成功");
        } catch (Exception e) {
            throw new RuntimeException("已存在相同的领英索引，不要重复创建", e);
        }
    }
    
//    @ApiOperation(value = "4、根据邮箱查询")
//    @GetMapping("/getByEmail")
//    public ResponseVO<LyUser> getByEmail(String email) {
//        LyUser lyUser = lyUserService.getFirstByEmail(email);
//        return ResponseVO.getSuccessResponseVo(lyUser);
//    }
//    
//    @ApiOperation(value = "5、根据姓名查询")
//    @GetMapping("/getByFullName")
//    public ResponseVO<List<LyUser>> getByFullName(String fullName) {
//        List<LyUser> lyUserList = lyUserService.getLikeFullName(fullName);
//        return ResponseVO.getSuccessResponseVo(lyUserList);
//    }
//    
//    @ApiOperation(value = "6、根据手机号查询")
//    @GetMapping("/getByMobile")
//    public ResponseVO<List<LyUser>> getByMobile(String mobile) {
//        if (mobile.matches("^1[3-9]\\d{9}$")) {
//            LyUser lyUser = lyUserService.getFirstByMobile(mobile);
//            List<LyUser> lyUserList = Collections.singletonList(lyUser);
//            return ResponseVO.getSuccessResponseVo(lyUserList);
//        } else {
//            List<LyUser> likeRightMobile = lyUserService.getLikeRightMobile(mobile);
//            return ResponseVO.getSuccessResponseVo(likeRightMobile);
//        }
//    }
//    
//    @ApiOperation(value = "7、根据国家查询")
//    @GetMapping("/getByLocationCountry")
//    public ResponseVO<List<LyUser>> getByLocationCountry(String locationCountry) {
//        List<LyUser> lyUserList = lyUserService.getByLocationCountry(locationCountry);
//        return ResponseVO.getSuccessResponseVo(lyUserList);
//    }
    @ApiOperationSupport(order = 40,includeParameters = {"industry","id","jobTitle"})
    @ApiOperation(value = "8、根据查询条件内容查询")
    @PostMapping("/getByForm")
    public ResponseVO<Page<LyUser>> getByForm(@RequestBody LyUserReq lyUserReq) {
        if (ObjectUtil.isEmpty(lyUserReq)) {
            return ResponseVO.getErrorResponseMsg("没有查询条件");
        }
        System.out.println(lyUserReq);
        Page<LyUser> lyUserPage = lyUserService.getByQuery(lyUserReq);
        return ResponseVO.getSuccessResponseVo(lyUserPage);
    }

    /**
     * 查询所有国家列表
     * @return
     */
    @ApiOperation(value = "9、查询所有国家列表")
    @GetMapping("/selectCountry")
    public ResponseVO<List<Country>> selectCountry() {
        List<Country> countryList = countryService.selectCountryList();
        return ResponseVO.getSuccessResponseVo(countryList);
    }

//    /**
//     * 根据国家名称查询国家
//     * @param countryName
//     * @return
//     */
//    @ApiOperation(value = "10、根据国家名称查询")
//    @GetMapping("/getCountryName")
//    public ResponseVO<List<Country>> getCountryName(@RequestParam("countryName") String countryName) {
//        List<Country> country = countryService.getByCountryName(countryName);
//        return ResponseVO.getSuccessResponseVo(country);
//    }

    /**
     * 添加单条领英数据
     */
    @ApiOperation(value = "11、添加单条领英数据")
    @PostMapping("/add")
    public ResponseVO<LyUser> add(@RequestBody LyUser lyUser) {
        boolean save = lyUserService.save(lyUser);
        if (!save) {
            return ResponseVO.getErrorResponseMsg("添加失败");
        }
        return ResponseVO.getSuccessResponseVo(lyUser);
    }

    /**
     * 根据id批量删除
     */
    @ApiOperation(value = "12、根据id批量删除")
    @PostMapping("/deleteByIds")
    public ResponseVO<String> deleteByIds(@RequestBody List<Long> ids) {
        boolean removeByIds = lyUserService.removeByIds(ids);
        if (!removeByIds) {
            return ResponseVO.getErrorResponseMsg("删除失败");
        }
        return ResponseVO.getSuccessResponseVo("删除成功");
    }
    
    @ApiOperation(value = "13、根据id批量更新")
    @PostMapping("/updateByIds")
    public ResponseVO<String> updateByIds(@RequestBody List<LyUser> lyUserList) {
        boolean updateBatchById = lyUserService.updateBatchById(lyUserList);
        if (!updateBatchById) {
            return ResponseVO.getErrorResponseMsg("更新失败");
        }
        return ResponseVO.getSuccessResponseVo("更新成功");
    }

    @ApiOperation("根据国家名查询州列表")
    @GetMapping("/getProvinceByCountryName")
    public ResponseVO<List<Country>> getProvinceByCountryName(@ApiParam(value = "国家名", required = true) String countryName) {
        List<Country> provinceByCountryName = countryService.getProvinceByCountryName(countryName);
        return ResponseVO.getSuccessResponseVo(provinceByCountryName);
    }
    @ApiOperation("根据国家id查询州列表")
    @GetMapping("/getProvinceByCountryId")
    public ResponseVO<List<Country>> getProvinceByCountryId(@ApiParam(value = "国家id", required = true) Integer countryId) {
        List<Country> provinceByCountryId = countryService.getProvinceByCountryId(countryId);
        return ResponseVO.getSuccessResponseVo(provinceByCountryId);
    }
    
    @ApiOperation("查询区域表树形结构")
    @GetMapping("/getTree")
    public ResponseVO<List<CountryVO>> getTree() {
        List<CountryVO> tree = countryService.getTree();
        return ResponseVO.getSuccessResponseVo(tree);
    }
    
    @ApiOperation("查询所有行业信息")
    @GetMapping ("/getAllIndustry")
    public ResponseVO<List<Industry>> getAllIndustry() {
        List<Industry> industries = industryService.getAllIndustry();
        return ResponseVO.getSuccessResponseVo(industries);
    }
    @ApiOperation("查询所有行业名列表")
    @GetMapping ("/getAllIndustryName")
    public ResponseVO<List<String>> getAllIndustryName() {
        return ResponseVO.getSuccessResponseVo(industryService.getAllIndustryName());
    }
    
    @ApiOperation("根据行业名称查询职位列表")
    @GetMapping ("/getJobTitleByIndustryName")
    public ResponseVO<List<String>> getJobTitleByIndustryName(@ApiParam(value = "行业名称", required = true) String industryName) {
        return ResponseVO.getSuccessResponseVo(industryService.getJobTitleByIndustryName(industryName));
    }

}

